package vn.edu.hut.soict.translate.dictionary.bilingual;

import java.util.ArrayList;
import java.util.List;

import vn.edu.hut.soict.translate.dictionary.Dictionary;

public class BilingualDictionaryWithPOS extends Dictionary<String, SimpleDictionaryEntry> {
	private static final char CROSS_REFERENCE_SIGN = '$';
	public static final String DICTIONARY_FILE = "res/dictionary/VEwPOSdictionary.txt";

	protected void addEntry(String word, SimpleDictionaryEntry content) {
		super.addEntry(word, content);
	}

	private List<SimpleDictionaryEntry> entry(String word) {
		List<SimpleDictionaryEntry> entries = new ArrayList<SimpleDictionaryEntry>();
		List<SimpleDictionaryEntry> contents = lookUp(word);
		if (contents == null) {
			return null;
		}
		for (SimpleDictionaryEntry entry : contents) {
			if (entry.getMeaning().charAt(0) != CROSS_REFERENCE_SIGN) {
				entries.add(entry);
			}
			else {
				String crossRef = entry.getMeaning().substring(1).replace('_', ' ');
				entries.addAll(entry(crossRef, entry.getPartOfSpeech()));
			}
		}
		return entries;
	}

	private List<SimpleDictionaryEntry> entry(String word, PartOfSpeech partOfSpeech) {
		List<SimpleDictionaryEntry> entries = entry(word);
		if (entries == null) {
			return null;
		}
		if (partOfSpeech == PartOfSpeech.UNKNOWN) {
			return entries;
		}
		else {
			List<SimpleDictionaryEntry> specificEntries = new ArrayList<SimpleDictionaryEntry>();
			for (SimpleDictionaryEntry meaning : entries) {
				if (meaning.getPartOfSpeech() == partOfSpeech) {
					specificEntries.add(meaning);
				}
			}
			return specificEntries;
		}
	}

	public List<String> meaning(String word, PartOfSpeech partOfSpeech) {
		List<SimpleDictionaryEntry> entries = entry(word, partOfSpeech);
		if (entries == null || entries.size() == 0) {
			entries = entry(word);
			System.err.printf("No result for: %s (%s)\n", word, partOfSpeech.toString());
		}
		if (entries == null || entries.size() == 0) {
			return null;
		}
		else {
			List<String> meanings = new ArrayList<String>();
			for (SimpleDictionaryEntry entry : entries) {
				meanings.add(entry.getMeaning());
			}
			return meanings;
		}
	}
}
